<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="IP地址">
              <a-input v-decorator="['ipAddress', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="请求类型">
              <a-input v-decorator="['requestMethod', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
        </a-row>
        <!--<a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="操作类型">
              <a-input v-decorator="['operateType_dictText', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="日志类型">
              <a-input v-decorator="['logType_dictText', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
        </a-row>-->
        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="请求时间">
              <a-input v-decorator="['requestTime', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="请求用户">
              <a-input v-decorator="['username', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row style="width: 100%;">
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="请求路径">
              <a-input v-decorator="['requestPath', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="耗时(ms)">
              <a-input v-decorator="['executeTime', {}]" :readOnly="disableSubmit"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row style="width: 100%;" >
          <a-col :span="24">
            <a-form-item
              :labelCol="labelColX1"
              :wrapperCol="wrapperColX1"
              label="请求方法">
              <a-textarea v-decorator="['methodName', {}]" :readOnly="disableSubmit" autoSize/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row style="width: 100%;">
          <a-col :span="24">
            <a-form-item
              :labelCol="labelColX1"
              :wrapperCol="wrapperColX1"
              label="日志内容">
              <a-textarea v-decorator="['logContent', {}]" :readOnly="disableSubmit" autoSize/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row style="width: 100%;">
          <a-col :span="24">
            <a-form-item
              :labelCol="labelColX1"
              :wrapperCol="wrapperColX1"
              label="请求参数">
              <a-textarea v-decorator="['requestBody',{}]" :rows="4" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { queryLogInfo } from '@views/sysConfig/log/service';
  export default {
    components: { },
    name: 'LogModal',
    data () {
      return {
        title: '操作',
        visible: false,
        disableSubmit: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        labelColX1: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperColX1: {
          xs: { span: 24 },
          sm: { span: 21 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
        },
        url: {
        }

      }
    },
    created () {
    },
    methods: {
       edit(record) {
        this.form.resetFields();
        this.queryLogInfo(record.id, record.requestTime).then((result) => {
          record.requestBody = result ? result.requestBody : '';
          this.model = Object.assign({}, record)
          this.visible = true
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,
              'ipAddress',
              'requestMethod',
              'requestTime',
              'username',
              'requestPath',
              'executeTime',
              'methodName',
              'logContent',
              'requestParam',
              'requestBody'
            ))
          })
         });
      },
       // 关闭
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      // 查询日志详情信息
      queryLogInfo(id, requestTime) {
        return new Promise(resolve => {
          queryLogInfo(id, { requestTimeStart: requestTime }).then(res => {
            if (res.success) {
              resolve(res.result);
            } else {
              resolve();
            }
          }).catch(() => {
            resolve();
          });
        });
      }
    }
  }
</script>

<style scoped>

</style>
